<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户修改</title>
    <meta name="keywords" content="后台用户添加">
    <meta name="description" content="后台用户添加">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="__ADMIN__css/bootstrap.min.css" rel="stylesheet">
    <link href="__ADMIN__css/font-awesome.min.css" rel="stylesheet">
    <link href="__ADMIN__css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="__ADMIN__css/animate.css" rel="stylesheet">
    <link href="__ADMIN__js/plugins/layer/skin/default/layer.css" rel="stylesheet">
    <link href="__ADMIN__css/style.css" rel="stylesheet">
    <link href="__ADMIN__css/plugins/switchery/switchery.css" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>修改管理用户</h5>
                </div>
                <div class="ibox-content">
                    <form method="post" id="formadd" name="formadd" class="form-horizontal" action="{:U('User/editsave')}" enctype="multipart/form-data">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">管理员名称：</label>
                            <div class="col-sm-10">
                                <!-- <input type="text" class="form-control" id="username" name="username" id="username" value="{$getAdmin.name}"> -->
                                {$getAdmin.name}
                            </div>
                            <input type="hidden" name="id" value="{$getAdmin.id}">
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">管理员角色：</label>

                            <div class="col-sm-10">
                                <select class="form-control" name="pid" id="pid" data-value="{$getAdmin.group_id}">
                                    <option value="">==请选择角色==</option>
                                    <volist name="getGroup" id="vo">
                                        <option value="{$vo.id}" <if condition="$getAdmin['group_id'] == $vo['id']">selected</if>>{$vo.title}</option>         
                                    </volist> 
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">上传头像：</label>
                            <div class="col-sm-10">
                                <input type="file" name="img" id="img"/>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">真实姓名：</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="realname" id="realname" value="{$getAdmin.realname}" data-value="{$getAdmin.realname}">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                       <!--  <div class="form-group">
                            <label class="col-sm-2 control-label">状态：</label>
                            <div class="col-sm-10">
                                <input type="checkbox" name="status" id="status" value="1" class="js-switch" data-value="{$getAdmin['status']}" <if condition="$getAdmin['status'] eq 1">checked</if>> 正常
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div> -->
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button class="btn btn-primary" type="submit" id="submit">确认修改</button>
                                <a class="btn btn-white" href="javascript:history.go(-1)">取消</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="__ADMIN__js/jquery.min.js"></script>
<script src="__ADMIN__js/bootstrap.min.js"></script>
<script src="__ADMIN__js/plugins/switchery/switchery.js"></script>
<script src="__ADMIN__js/plugins/layer/layer.js"></script>
<script src="__ADMIN__js/jquery.form.js"></script>
<script src="__ADMIN__js/comment.js"></script>
<script>
    $(function(){
        //第二种用jquery
        // $('#submit').click(function(){
        //     var pidObj = $('#pid'),oldpid = pidObj.data('value'),newpid = pidObj.val(),submit = false,
        //    data = new Object();
        //    data.id = $('[name="id"]').val();//声明一个对象，获取隐藏的id
        //    if(oldpid != newpid){
        //         if(newpid == ''){
        //             comment.error('请选择分组！');
        //             return false;
        //         }
        //         data.pid = newpid;
        //         submit = true;
        //    }
        //     var rnameObj = $('#realname'),oldrname = rnameObj.data('value'),newrname = rnameObj.val();
        //     if(oldrname != newrname){
        //         if(newrname == ''){
        //             comment.error('请填写真实姓名！');
        //             return false;
        //         }
        //         data.realname = newrname;
        //         submit = true;
        //     }else{
        //         rnameObj.removeAttr('name');
        //     }

        //     var newimg = $('#img').val();
        //     if(newimg != ''){
        //        data.img = newimg;
        //        submit = true; 
        //     }
        //     //原生的ajax不能提交图片，需要用formdata new 一个对象加上一些参数
        //     var forma = new FormData (document.getElementById('#formadd'));
        //     if(submit){
        //         $.ajax({
        //             url:$('#formadd').attr('action'),
        //             data:forma,
        //             dataType:'json',
        //             type:'post',
        //             cache: false,
        //             processData: false,
        //             contentType: false,
        //             success:function(data){
        //                 console.log(data);
        //             }
        //         });
        //     }
        //     comment.success('修改成功！',"{:U('User/index')}");
        //     return false;
        // });


        //第一种用ajaxform
        //前台ajax验证是否改动没有改动不需要操作数据库
        $('#formadd').ajaxForm({
            beforeSerialize:changeValue,
            beforeSubmit:checkForm,
            success:complete,
            dataType:'json'
        });
        //提交之前需要判断填的
        function checkForm(){

        }
        function changeValue(){
            //隐藏的group_id就是pid
            var pidObj = $('#pid'),oldpid = pidObj.data('value'),newpid = pidObj.val(),submit = false;
            //如果oldpid 和 newpid 相等的话是没有修改无需操作数据库，不相等的话是修改了修改的不能为空
            if(newpid != oldpid){
                if(newpid == ''){
                    comment.error('请选择分组！');
                    return false;
                }
                submit = true;//提交成功
            }else{
                pidObj.removeAttr('name');//相等话需要移除name的属性
            }
            var rnameObj = $('#realname'),oldrname = rnameObj.data('value'),newrname = rnameObj.val();
            if(oldrname != newrname){
                if(newrname == ''){
                    comment.error('请填写真实姓名！');
                    return false;
                }
                submit = true;
            }else{
                rnameObj.removeAttr('name');
            }
            var newPic = $('#img').val();
            if(newPic != ''){
                submit = true;
            }
            //如果没有修改就直接保存让他修成功
            if(!submit){
                comment.success('修改成功！',"{:U('User/index')}");
                return false;
            }
        }
        function complete(data){
            if(data.status < 1){
                comment.error(data.mess);
            }else{
                comment.success(data.mess,"{:U('User/index')}");
            }
        }
    });
    var elem = document.querySelector('.js-switch');
    var init = new Switchery(elem,{size: 'small', jackColor: '#9decff' });  
</script>
</body>
</html>